Imports System.Data.SqlClient

Public Class ViewCommitteeChair
    Inherits System.Web.UI.Page
    Protected protectedstrMemberID As String
    Protected WithEvents txtDirectorID As System.Web.UI.WebControls.TextBox
    Protected protectedstrDirectorID As String

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lbl1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents dtgNTCMembers As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'only allow Moderators or Admins to view
        If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleUser")) Or _
            User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) _
       Then
        Else
            Response.Redirect("")
        End If

        If Not Page.IsPostBack Then
            BindTheGrid()
        End If
        'load the grids only on the initial pageload

    End Sub


    Public Sub BindTheGrid()
        Dim strSql As String
        Dim objConn As SqlConnection = New SqlConnection((ConfigurationManager.AppSettings.Item("SQLConnectionString")))

        strSql = "sp_nena_Committee_Chairs_full"

        Dim cmdCustomers As SqlCommand = New SqlCommand(strSql, objConn)

        cmdCustomers.CommandType = CommandType.Text

        objConn.Open()
        dtgNTCMembers.DataSource = cmdCustomers.ExecuteReader()

        dtgNTCMembers.DataBind()
        objConn.Close()

    End Sub

    Public Function BindMembers()
        'create counselors dropdown datasource
        Dim strSQL As String
        Dim objConn As SqlConnection = New SqlConnection((ConfigurationManager.AppSettings.Item("SQLConnectionString")))

        strSQL = "select * from Members_Committee where " & _
       "Active_Status = '" & ConfigurationManager.AppSettings.Item("Active_Status_Active") & "'"

        Dim cmdCustomers As SqlCommand = New SqlCommand(strSQL, objConn)

        cmdCustomers.CommandType = CommandType.Text
        objConn.Open()
        Return cmdCustomers.ExecuteReader(CommandBehavior.CloseConnection)

    End Function

    Public Sub SetDropDownIndex(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim ed As System.Web.UI.WebControls.DropDownList
        ed = sender
        ed.SelectedIndex = protectedstrMemberID

    End Sub

    Private Sub dtgAppointments_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dtgNTCMembers.ItemCommand

        protectedstrMemberID = CType(e.Item.FindControl("lblMemberID"), Label).Text
        protectedstrDirectorID = CType(e.Item.FindControl("lblDirectorID"), Label).Text
        'need to change the ID variables

        Select Case e.CommandName
            Case "View"
                dtgNTCMembers.EditItemIndex = -1
                'remove the edit index
                BindTheGrid()
                'refresh counselor display
        End Select

        dtgNTCMembers.SelectedIndex = e.Item.ItemIndex
        'set the selectedindex to apply 
        'selecteditem formatting

        txtDirectorID.Text = protectedstrMemberID
        'store the schedule ID for update

    End Sub

    Private Sub dtgNTCMembers_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dtgNTCMembers.EditCommand
        protectedstrMemberID = CType(e.Item.FindControl("lblMemberID"), Label).Text
        protectedstrDirectorID = CType(e.Item.FindControl("lblDirectorID"), Label).Text
        'need to change the ID variables

        dtgNTCMembers.EditItemIndex = e.Item.ItemIndex
        'set details index
        BindTheGrid()
        'refresh grid
        txtDirectorID.Text = protectedstrMemberID

    End Sub

    Private Sub dtgNTCMembers_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dtgNTCMembers.CancelCommand
        dtgNTCMembers.EditItemIndex = -1
        'remove the edititemindex
        BindTheGrid()
        'refresh the grid
    End Sub

    Private Sub dtgNTCMembers_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dtgNTCMembers.UpdateCommand

        Dim objConn As SqlConnection = New SqlConnection((ConfigurationManager.AppSettings.Item("SQLConnectionString")))
        Dim cmdCustomers As SqlCommand
        Dim strDirectorID As String = CType(e.Item.FindControl("lblDirectorID"), Label).Text
        Dim strMemberID As String = CType(e.Item.FindControl("ddlMemberName"), DropDownList).SelectedItem.Value
        Dim strTitle As String = CType(e.Item.FindControl("txtTitle"), TextBox).Text
        Dim strCommitteeEmail As String = CType(e.Item.FindControl("txtCommitteeEmail"), TextBox).Text
        Dim strSql As String


        strSql = "UPDATE Directors_Chair SET Members_Committee_ID='" & strMemberID _
               & "', Description = '" & strTitle & "',CommitteeEmail = '" & strCommitteeEmail & "' WHERE ID='" & strDirectorID & "'"
        cmdCustomers = New SqlCommand(strSql, objConn)
        objConn.Open()
        cmdCustomers.ExecuteReader(CommandBehavior.CloseConnection)
        dtgNTCMembers.EditItemIndex = -1
        BindTheGrid()


    End Sub
End Class
